const {
    getBikeListByBrandIdServe,
    getBikeByIdServe,
    updateBikeScoreServe,
    getBikeScoreRanKeListServe,
    getRecentlyUpdatedListServe,
    getIndividualizationListServe
} = require('../serve/bike.serve')
const {getBikeCommentListServe} = require('../serve/comment.serve')
const {getCollectByUserIdServe} = require('../serve/appletBikeCollect.serve')
const {getBikeScoreServe} = require('../serve/appletBikeScore.serve')
const {scoreFilter} = require('../utils/index')

class AppletBikeController {
    async getBikeListByBrandId(ctx) {
        try {
            const res = await getBikeListByBrandIdServe(ctx)
            ctx.body = {
                msg: '获取成功',
                status: 0,
                data: {
                    total: res.total,
                    pageSize: res.pageSize,
                    pageNum: res.pageNum,
                    pageData: res.data
                }
            }
        } catch (e) {
            ctx.body = {
                msg: e.message,
                status: 1,
            }
        }

    }

    async getBikeDetailByBikeId(ctx) {
        try {
            const {bikeId} = ctx.request.query;
            console.log(ctx.state);
            const url = ctx.request.url
            if (bikeId) {
                const {id: userId} = ctx.state.appletUser
                let scoreLevel = []
                let scoreNum = 0
                let overallScore = 0
                let res = await getBikeByIdServe(bikeId, true);
                let commentList = await getBikeCommentListServe(bikeId,url.includes('wx'))
                if (res.appletBikeScores.length > 0) {
                    [1, 2, 3, 4, 5].forEach(item => {
                        const score = scoreFilter([...res.appletBikeScores], item)
                        scoreNum += score.length
                        scoreLevel.push(score.length)
                    })
                    scoreLevel = scoreLevel.map(score => {
                        return {
                            score,
                            scorePercentage:
                                Math.round(score / scoreNum * 100)
                        }
                    })
                    //根据评星比例计算出综合评分
                    scoreLevel.forEach((score, index) => {
                        overallScore += (score.scorePercentage / 100) * ((index + 1) * 2)
                    })
                    await updateBikeScoreServe(bikeId, overallScore.toFixed(1))
                    delete res.appletBikeScores
                }
                const currentUserScore = await getBikeScoreServe(ctx)
                const collectBike = await getCollectByUserIdServe({bikeId, userId})
                const data = {
                    ...res,
                    isCollect: collectBike ? true : false,
                    scoreLevel,
                    isScore: currentUserScore.length > 0 ? true : false,
                    score: currentUserScore.length > 0 ? currentUserScore[0].score.toFixed(1) : 0,
                    scoreNum: overallScore.toFixed(1),
                    commentList,
                    isShowBtn: false
                }
                ctx.body = {
                    msg: '获取成功',
                    status: 0,
                    data
                }
            } else {
                throw ({message: '缺少必要参数'})
            }
        } catch (e) {
            console.log(e)
            ctx.body = {
                msg: e.message,
                status: 1,
            }
        }
    }

    async getBikeScoreRanKeList(ctx) {
        try {
            const res = await getBikeScoreRanKeListServe()
            ctx.body = {
                msg: '获取成功',
                status: 0,
                data: res
            }
        } catch (e) {
            ctx.body = {
                msg: '获取失败',
                status: e.message,
            }
        }
    }

    async getRecentlyUpdatedList(ctx) {
        try {
            const res = await getRecentlyUpdatedListServe()
            ctx.body = {
                msg: '获取成功',
                status: 0,
                data: res
            }
        } catch (e) {
            ctx.body = {
                msg: '获取失败',
                status: e.message,
            }
        }
    }

    async getIndividualizationList(ctx) {
        try {
            const res = await getIndividualizationListServe(ctx)
            ctx.body = {
                msg: '获取成功',
                status: 0,
                data: res
            }
        } catch (e) {
            ctx.body = {
                msg: '获取失败',
                status: e.message,
            }
        }
    }
}

module.exports = new AppletBikeController();
